<?php

namespace app\common\constant;

/**
 * 错误码常量类
 */
class ErrorCode
{
    // 系统级错误码 (1000-1999)
    public const SYSTEM_ERROR = 1000;
    public const SYSTEM_BUSY = 1001;
    public const SYSTEM_TIMEOUT = 1002;
    public const SYSTEM_MAINTENANCE = 1003;

    // 参数错误 (2000-2999)
    public const PARAM_ERROR = 2000;
    public const PARAM_MISSING = 2001;
    public const PARAM_INVALID = 2002;
    public const PARAM_TYPE_ERROR = 2003;

    // 认证授权错误 (3000-3999)
    public const AUTH_ERROR = 3000;
    public const AUTH_TOKEN_MISSING = 3001;
    public const AUTH_TOKEN_INVALID = 3002;
    public const AUTH_TOKEN_EXPIRED = 3003;
    public const AUTH_PERMISSION_DENIED = 3004;
    public const AUTH_LOGIN_REQUIRED = 3005;

    // 业务逻辑错误 (4000-4999)
    public const BUSINESS_ERROR = 4000;
    public const MERCHANT_NOT_FOUND = 4001;
    public const MERCHANT_STATUS_ERROR = 4002;
    public const MERCHANT_ALREADY_EXISTS = 4003;
    public const UPLOAD_FILE_ERROR = 4004;
    public const OCR_RECOGNITION_ERROR = 4005;
    public const BANK_INFO_ERROR = 4006;

    // 第三方服务错误 (5000-5999)
    public const THIRD_PARTY_ERROR = 5000;
    public const WECHAT_API_ERROR = 5001;
    public const ALIPAY_API_ERROR = 5002;
    public const OSS_UPLOAD_ERROR = 5003;

    // 数据库错误 (6000-6999)
    public const DATABASE_ERROR = 6000;
    public const DATABASE_CONNECTION_ERROR = 6001;
    public const DATABASE_QUERY_ERROR = 6002;
    public const DATABASE_TRANSACTION_ERROR = 6003;

    // 错误信息映射
    public const ERROR_MESSAGES = [
        self::SYSTEM_ERROR => '系统错误',
        self::SYSTEM_BUSY => '系统繁忙，请稍后重试',
        self::SYSTEM_TIMEOUT => '请求超时',
        self::SYSTEM_MAINTENANCE => '系统维护中',

        self::PARAM_ERROR => '参数错误',
        self::PARAM_MISSING => '缺少必要参数',
        self::PARAM_INVALID => '参数格式不正确',
        self::PARAM_TYPE_ERROR => '参数类型错误',

        self::AUTH_ERROR => '认证失败',
        self::AUTH_TOKEN_MISSING => '缺少认证令牌',
        self::AUTH_TOKEN_INVALID => '认证令牌无效',
        self::AUTH_TOKEN_EXPIRED => '认证令牌已过期',
        self::AUTH_PERMISSION_DENIED => '权限不足',
        self::AUTH_LOGIN_REQUIRED => '请先登录',

        self::BUSINESS_ERROR => '业务处理失败',
        self::MERCHANT_NOT_FOUND => '商户信息不存在',
        self::MERCHANT_STATUS_ERROR => '商户状态异常',
        self::MERCHANT_ALREADY_EXISTS => '商户已存在',
        self::UPLOAD_FILE_ERROR => '文件上传失败',
        self::OCR_RECOGNITION_ERROR => '图片识别失败',
        self::BANK_INFO_ERROR => '银行信息错误',

        self::THIRD_PARTY_ERROR => '第三方服务异常',
        self::WECHAT_API_ERROR => '微信接口调用失败',
        self::ALIPAY_API_ERROR => '支付宝接口调用失败',
        self::OSS_UPLOAD_ERROR => '文件存储失败',

        self::DATABASE_ERROR => '数据库操作失败',
        self::DATABASE_CONNECTION_ERROR => '数据库连接失败',
        self::DATABASE_QUERY_ERROR => '数据库查询失败',
        self::DATABASE_TRANSACTION_ERROR => '数据库事务失败',
    ];

    /**
     * 获取错误信息
     * @param int $code
     * @return string
     */
    public static function getMessage(int $code): string
    {
        return self::ERROR_MESSAGES[$code] ?? '未知错误';
    }
}
